package edu.chl.da.ejb.core;

import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * This class respresent a flight in the system.
 * @author Jakob
 */
@Entity
@Table(name = "FLIGHT")
@NamedQueries({
    @NamedQuery(name = "Flight.findAll", query = "SELECT f FROM Flight f"),
    @NamedQuery(name = "Flight.findByFromairport", query = "SELECT f FROM Flight f WHERE f.flightPK.fromairport = :fromairport"),
    @NamedQuery(name = "Flight.findByFromdestination", query = "SELECT f FROM Flight f WHERE f.flightPK.fromdestination = :fromdestination"),
    @NamedQuery(name = "Flight.findByAirplane", query = "SELECT f FROM Flight f WHERE f.flightPK.airplane = :airplane"),
    @NamedQuery(name = "Flight.findByToairport", query = "SELECT f FROM Flight f WHERE f.toairport = :toairport"),
    @NamedQuery(name = "Flight.findByTodestination", query = "SELECT f FROM Flight f WHERE f.todestination = :todestination"),
    @NamedQuery(name = "Flight.findByPrice", query = "SELECT f FROM Flight f WHERE f.price = :price"),
    @NamedQuery(name = "Flight.findByStartdate", query = "SELECT f FROM Flight f WHERE f.startdate = :startdate"),
    @NamedQuery(name = "Flight.findByStarttime", query = "SELECT f FROM Flight f WHERE f.starttime = :starttime"),
    @NamedQuery(name = "Flight.findByEnddate", query = "SELECT f FROM Flight f WHERE f.enddate = :enddate"),
    @NamedQuery(name = "Flight.findByEndtime", query = "SELECT f FROM Flight f WHERE f.endtime = :endtime")})
public class Flight implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected FlightPK flightPK;
    @Column(name = "TOAIRPORT")
    private String toairport;
    @Column(name = "TODESTINATION")
    private String todestination;
    @Column(name = "PRICE")
    private Double price;
    @Column(name = "STARTDATE")
    private String startdate;
    @Column(name = "STARTTIME")
    private String starttime;
    @Column(name = "ENDDATE")
    private String enddate;
    @Column(name = "ENDTIME")
    private String endtime;
    @JoinColumn(name = "AIRPLANE", referencedColumnName = "ID", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Airplane airplane1;
    @JoinColumns({
        @JoinColumn(name = "FROMAIRPORT", referencedColumnName = "NAME", insertable = false, updatable = false),
        @JoinColumn(name = "FROMDESTINATION", referencedColumnName = "DESTINATION", insertable = false, updatable = false)})
    @ManyToOne(optional = false)
    private Airport airport;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "flight")
    private List<Reservation> reservationList;

    public Flight() {
    }

    public Flight(FlightPK flightPK) {
        this.flightPK = flightPK;
    }

    public Flight(String fromairport, String fromdestination, int airplane) {
        this.flightPK = new FlightPK(fromairport, fromdestination, airplane);
    }

    public FlightPK getFlightPK() {
        return flightPK;
    }

    public void setFlightPK(FlightPK flightPK) {
        this.flightPK = flightPK;
    }

    public String getToairport() {
        return toairport;
    }

    public void setToairport(String toairport) {
        this.toairport = toairport;
    }

    public String getTodestination() {
        return todestination;
    }

    public void setTodestination(String todestination) {
        this.todestination = todestination;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getStartdate() {
        return startdate;
    }

    public void setStartdate(String startdate) {
        this.startdate = startdate;
    }

    public String getStarttime() {
        return starttime;
    }

    public void setStarttime(String starttime) {
        this.starttime = starttime;
    }

    public String getEnddate() {
        return enddate;
    }

    public void setEnddate(String enddate) {
        this.enddate = enddate;
    }

    public String getEndtime() {
        return endtime;
    }

    public void setEndtime(String endtime) {
        this.endtime = endtime;
    }

    public Airplane getAirplane1() {
        return airplane1;
    }

    public void setAirplane1(Airplane airplane1) {
        this.airplane1 = airplane1;
    }

    public Airport getAirport() {
        return airport;
    }

    public void setAirport(Airport airport) {
        this.airport = airport;
    }

    public List<Reservation> getReservationList() {
        return reservationList;
    }

    public void setReservationList(List<Reservation> reservationList) {
        this.reservationList = reservationList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (flightPK != null ? flightPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Flight)) {
            return false;
        }
        Flight other = (Flight) object;
        if ((this.flightPK == null && other.flightPK != null) || (this.flightPK != null && !this.flightPK.equals(other.flightPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "edu.chl.da.ejb.core.Flight[flightPK=" + flightPK + "]";
    }

}
